package org.dmcs.homework.statistics.implementations;

public class VectorUtil {

	public double[] add(double[] v1, double[] v2) {
		if(v1 == null || v2 == null) {
			throw new IllegalArgumentException("VectorUtil|add|v1 or v2 is null");
		}
		else if(v1.length != v2.length) {
			throw new IllegalArgumentException("VectorUtil|add|vector lengths not "
					+ "matching");
		}
		
		double[] result = new double[v1.length];
		
		for(int i=0; i<result.length; i++) {
			result[i] = v1[i] + v2[i];
		}
 		
		return result;
	}
	
	public double[] subtract(double[] v1, double[] v2) {
		if(v1 == null || v2 == null) {
			throw new IllegalArgumentException("VectorUtil|subtract|v1 or v2 is null");
		}
		else if(v1.length != v2.length) {
			throw new IllegalArgumentException("VectorUtil|subtract|vector lengths not "
					+ "matching");
		}
		
		double[] result = new double[v1.length];
		
		for(int i=0; i<result.length; i++) {
			result[i] = v1[i] - v2[i];
		}
 		
		return result;
	}
	
	public void multiply(double[] v, double constant) {
		if(v == null) {
			throw new IllegalArgumentException("VectorUtil|multiply|the input vector"
					+ " is null");
		}
		
		for(int i=0; i<v.length; i++) {
			v[i] *= constant;
		}
	}
	
	public double length(double[] v) {
		double result = 0;
		
		for(int i=0; i<v.length; i++) {
			result += v[i]*v[i];
		}
		
		return result;
	}
}